package com.springframework.beans.factory.support;

import org.springframework.beans.factory.support.MethodOverride;
import org.springframework.beans.factory.support.MethodOverrides;
import org.springframework.lang.Nullable;

import java.lang.reflect.Method;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @Author 虎哥
 * @Description //TODO
 * 要带着问题去学习,多猜想多验证
 **/
public class MyMethodOverrides {
    private final Set<MyMethodOverride> overrides = new CopyOnWriteArraySet<>();


    /**
     * Create new MethodOverrides.
     */
    public MyMethodOverrides() {
    }

    /**
     * Deep copy constructor.
     */
    public MyMethodOverrides(MyMethodOverrides other) {
        addOverrides(other);
    }


    /**
     * Copy all given method overrides into this object.
     */
    public void addOverrides(@Nullable MyMethodOverrides other) {
        if (other != null) {
            this.overrides.addAll(other.overrides);
        }
    }

    /**
     * Add the given method override.
     * @param override
     */
    public void addOverride(MyReplaceOverride override) {
        this.overrides.add(override);
    }

    /**
     * Return all method overrides contained by this object.
     * @return a Set of MethodOverride objects
     * @see MethodOverride
     */
    public Set<MyMethodOverride> getOverrides() {
        return this.overrides;
    }

    /**
     * Return whether the set of method overrides is empty.
     */
    public boolean isEmpty() {
        return this.overrides.isEmpty();
    }

    /**
     * Return the override for the given method, if any.
     * @param method method to check for overrides for
     * @return the method override, or {@code null} if none
     */
    @Nullable
    public MyMethodOverride getOverride(Method method) {
        MyMethodOverride match = null;
        for (MyMethodOverride candidate : this.overrides) {
            if (candidate.matches(method)) {
                match = candidate;
            }
        }
        return match;
    }


}
